﻿using System.Collections;
using UnityEngine;

namespace UFrame
{
    public class OUDown : MonoBehaviour
    {
        public float displayTime = 0;

        private float counter;
        public SpriteRenderer sr;
        public Rigidbody2D c2d;
        private bool isState = false;
        public float downForce = -20f;


        public void Awake()
        {
            if (sr == null)
                sr = GetComponent<SpriteRenderer>();
            if (c2d == null)
                c2d = GetComponent<Rigidbody2D>();

            counter = Time.realtimeSinceStartup;
            if (sr != null)
                sr.enabled = false;
            c2d.isKinematic = true;
        }

        public void Update()
        {
            if (Input.GetKeyDown(KeyCode.A))
            {
                c2d.AddForce(new Vector2(0, downForce), ForceMode2D.Impulse);
                Debug.Log("设置力度A" + downForce);
            }

            if (isState)
                return;

            float cha = Time.realtimeSinceStartup - counter;
            if (cha >= displayTime)
            {
                if (sr != null)
                    sr.enabled = true;
                c2d.isKinematic = false;
                isState = true;
//            Debug.Log("使用力度");
                StartCoroutine(Down());
            }
        }


        private IEnumerator Down()
        {
            yield return null;
            c2d.AddForce(new Vector2(0, downForce), ForceMode2D.Impulse);
            DestroyImmediate(this);
        }
    }
}